import { get, post, put, del } from '@/utils/request'

// 获取客户列表
export function getCustomersList(params = {}) {
  return get('/customers', params)
}

// 获取客户详情
export function getCustomerDetail(id) {
  return get(`/customers/${id}`)
}

// 创建客户
export function createCustomer(data) {
  return post('/customers', data, {
    showLoading: true,
    loadingText: '创建客户中...'
  })
}

// 更新客户信息
export function updateCustomer(id, data) {
  return put(`/customers/${id}`, data, {
    showLoading: true,
    loadingText: '更新客户信息...'
  })
}

// 删除客户
export function deleteCustomer(id) {
  return del(`/customers/${id}`, {}, {
    showLoading: true,
    loadingText: '删除客户...'
  })
}

// 获取客户统计
export function getCustomerStats() {
  return get('/customers/stats')
}

// 获取客户订单历史
export function getCustomerOrders(customerId, params = {}) {
  return get(`/customers/${customerId}/orders`, params)
}

// 添加客户备注
export function addCustomerNote(customerId, note) {
  return post(`/customers/${customerId}/notes`, { note })
}

// 获取客户备注列表
export function getCustomerNotes(customerId) {
  return get(`/customers/${customerId}/notes`)
}

// 设置客户等级
export function setCustomerLevel(customerId, level) {
  return put(`/customers/${customerId}/level`, { level })
}

// 加入黑名单
export function addToBlacklist(customerId, reason) {
  return post(`/customers/${customerId}/blacklist`, { reason })
}

// 移出黑名单
export function removeFromBlacklist(customerId) {
  return del(`/customers/${customerId}/blacklist`)
}

// 批量操作客户
export function batchUpdateCustomers(customerIds, action, data = {}) {
  return put('/customers/batch', {
    customerIds,
    action, // 'level', 'blacklist', 'delete'
    data
  })
}

// 导出客户数据
export function exportCustomersData(params = {}) {
  return get('/customers/export', params, {
    responseType: 'blob'
  })
}

// 获取客户分析数据
export function getCustomerAnalysis(params = {}) {
  return get('/customers/analysis', params)
}

// 获取客户画像
export function getCustomerProfile(customerId) {
  return get(`/customers/${customerId}/profile`)
}

// 获取客户消费趋势
export function getCustomerConsumptionTrend(customerId, timeRange = '30d') {
  return get(`/customers/${customerId}/consumption-trend`, { timeRange })
}

// 获取客户满意度
export function getCustomerSatisfaction(customerId) {
  return get(`/customers/${customerId}/satisfaction`)
}

// 获取客户标签
export function getCustomerTags(customerId) {
  return get(`/customers/${customerId}/tags`)
}

// 更新客户标签
export function updateCustomerTags(customerId, tags) {
  return put(`/customers/${customerId}/tags`, { tags })
}

// 搜索客户
export function searchCustomers(keyword, filters = {}) {
  return get('/customers/search', {
    keyword,
    ...filters
  })
}

// 获取客户地理分布
export function getCustomerGeographicDistribution() {
  return get('/customers/geographic-distribution')
}

// 获取客户年龄分布
export function getCustomerAgeDistribution() {
  return get('/customers/age-distribution')
}

// 获取客户消费能力分析
export function getCustomerSpendingAnalysis() {
  return get('/customers/spending-analysis')
}

// 获取客户流失分析
export function getCustomerChurnAnalysis() {
  return get('/customers/churn-analysis')
}

// 获取客户生命周期价值
export function getCustomerLifetimeValue(customerId) {
  return get(`/customers/${customerId}/lifetime-value`)
}

// 获取相似客户推荐
export function getSimilarCustomers(customerId) {
  return get(`/customers/${customerId}/similar`)
}

// 发送客户消息
export function sendCustomerMessage(customerId, message) {
  return post(`/customers/${customerId}/messages`, message)
}

// 获取客户消息历史
export function getCustomerMessages(customerId, params = {}) {
  return get(`/customers/${customerId}/messages`, params)
}

// 客户状态常量
export const CUSTOMER_STATUS = {
  NORMAL: 'NORMAL',
  VIP: 'VIP',
  BLACKLIST: 'BLACKLIST',
  INACTIVE: 'INACTIVE'
}

export const CUSTOMER_LEVEL = {
  NORMAL: 'NORMAL',
  BRONZE: 'BRONZE',
  SILVER: 'SILVER',
  GOLD: 'GOLD',
  PLATINUM: 'PLATINUM',
  DIAMOND: 'DIAMOND',
  VIP: 'VIP'
}

export const CUSTOMER_GENDER = {
  MALE: 'MALE',
  FEMALE: 'FEMALE',
  OTHER: 'OTHER'
}

// 状态文本映射
export const CUSTOMER_STATUS_TEXT = {
  [CUSTOMER_STATUS.NORMAL]: '正常',
  [CUSTOMER_STATUS.VIP]: 'VIP',
  [CUSTOMER_STATUS.BLACKLIST]: '黑名单',
  [CUSTOMER_STATUS.INACTIVE]: '不活跃'
}

export const CUSTOMER_LEVEL_TEXT = {
  [CUSTOMER_LEVEL.NORMAL]: '普通客户',
  [CUSTOMER_LEVEL.BRONZE]: '铜牌客户',
  [CUSTOMER_LEVEL.SILVER]: '银牌客户',
  [CUSTOMER_LEVEL.GOLD]: '金牌客户',
  [CUSTOMER_LEVEL.PLATINUM]: '白金客户',
  [CUSTOMER_LEVEL.DIAMOND]: '钻石客户',
  [CUSTOMER_LEVEL.VIP]: 'VIP客户'
}

export const CUSTOMER_GENDER_TEXT = {
  [CUSTOMER_GENDER.MALE]: '男',
  [CUSTOMER_GENDER.FEMALE]: '女',
  [CUSTOMER_GENDER.OTHER]: '其他'
}

// 状态颜色映射
export const CUSTOMER_STATUS_COLOR = {
  [CUSTOMER_STATUS.NORMAL]: '',
  [CUSTOMER_STATUS.VIP]: 'warning',
  [CUSTOMER_STATUS.BLACKLIST]: 'danger',
  [CUSTOMER_STATUS.INACTIVE]: 'info'
}

export const CUSTOMER_LEVEL_COLOR = {
  [CUSTOMER_LEVEL.NORMAL]: '',
  [CUSTOMER_LEVEL.BRONZE]: 'warning',
  [CUSTOMER_LEVEL.SILVER]: 'info',
  [CUSTOMER_LEVEL.GOLD]: 'warning',
  [CUSTOMER_LEVEL.PLATINUM]: 'success',
  [CUSTOMER_LEVEL.DIAMOND]: 'primary',
  [CUSTOMER_LEVEL.VIP]: 'danger'
}
